home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / sys5 / iscwmpst.z / iscwmpst / tcp / src / mbuf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-10  |  10.2 KB  |  496 lines

  1. /* @(#) $Header: mbuf.c,v 1.6 91/05/09 07:38:38 deyke Exp $ */
  2.  
  3. /* mbuf (message buffer) primitives
  4.  * Copyright 1991 Phil Karn, KA9Q
  5.  */
  6. #include <stdio.h>
  7. #include "global.h"
  8. #include "mbuf.h"
  9. #include "proc.h"
  10.  
  11. /* Allocate mbuf with associated buffer of 'size' bytes. If interrupts
  12.  * are enabled, use the regular heap. If they're off, use the special
  13.  * interrupt buffer pool.
  14.  */
  15. struct mbuf *
  16. alloc_mbuf(size)
  17. register int16 size;
  18. {
  19.     register struct mbuf *bp;
  20.  
  21.         /* Interrupts are enabled, use the heap normally */
  22.         bp = (struct mbuf *)malloc((unsigned)(size + sizeof(struct mbuf)));
  23.         if(bp == NULLBUF)
  24.             return NULLBUF;
  25.         /* Clear just the header portion */
  26.         memset((char *)bp,0,sizeof(struct mbuf));
  27.         if((bp->size = size) != 0)
  28.             bp->data = (char *)(bp + 1);
  29.         bp->refcnt++;
  30.     return bp;
  31. }
  32. /* Allocate mbuf, waiting if memory is unavailable */
  33. struct mbuf *
  34. ambufw(size)
  35. int16 size;
  36. {
  37.     register struct mbuf *bp;
  38.  
  39.     bp = (struct mbuf *)mallocw((unsigned)(size + sizeof(struct mbuf)));
  40.  
  41.     /* Clear just the header portion */
  42.     memset((char *)bp,0,sizeof(struct mbuf));
  43.     if((bp->size = size) != 0)
  44.         bp->data = (char *)(bp + 1);
  45.     bp->refcnt++;
  46.     return bp;
  47. }
  48.  
  49. /* Decrement the reference pointer in an mbuf. If it goes to zero,
  50.  * free all resources associated with mbuf.
  51.  * Return pointer to next mbuf in packet chain
  52.  */
  53. struct mbuf *
  54. free_mbuf(bp)
  55. register struct mbuf *bp;
  56. {
  57.     struct mbuf *bpnext;
  58.  
  59.     if(bp == NULLBUF)
  60.         return NULLBUF;
  61.  
  62.     bpnext = bp->next;
  63.     if(bp->dup != NULLBUF){
  64.         free_mbuf(bp->dup);     /* Follow indirection */
  65.         bp->dup = NULLBUF;
  66.     }
  67.     /* Decrement reference count. If it has gone to zero, free it. */
  68.     if(--bp->refcnt <= 0){
  69.             free((char *)bp);
  70.     }
  71.     return bpnext;
  72. }
  73.  
  74. /* Free packet (a chain of mbufs). Return pointer to next packet on queue,
  75.  * if any
  76.  */
  77. struct mbuf *
  78. free_p(bp)
  79. register struct mbuf *bp;
  80. {
  81.     register struct mbuf *abp;
  82.  
  83.     if(bp == NULLBUF)
  84.         return NULLBUF;
  85.     abp = bp->anext;
  86.     while(bp != NULLBUF)
  87.         bp = free_mbuf(bp);
  88.     return abp;
  89. }
  90. /* Free entire queue of packets (of mbufs) */
  91. void
  92. free_q(q)
  93. struct mbuf **q;
  94. {
  95.     register struct mbuf *bp;
  96.  
  97.     while((bp = dequeue(q)) != NULLBUF)
  98.         free_p(bp);
  99. }
  100.  
  101. /* Count up the total number of bytes in a packet */
  102. int16
  103. len_p(bp)
  104. register struct mbuf *bp;
  105. {
  106.     register int16 cnt = 0;
  107.  
  108.     while(bp != NULLBUF){
  109.         cnt += bp->cnt;
  110.         bp = bp->next;
  111.     }
  112.     return cnt;
  113. }
  114. /* Count up the number of packets in a queue */
  115. int16
  116. len_q(bp)
  117. register struct mbuf *bp;
  118. {
  119.     register int16 cnt;
  120.  
  121.     for(cnt=0;bp != NULLBUF;cnt++,bp = bp->anext)
  122.         ;
  123.     return cnt;
  124. }
  125. /* Trim mbuf to specified length by lopping off end */
  126. void
  127. trim_mbuf(bpp,length)
  128. struct mbuf **bpp;
  129. int16 length;
  130. {
  131.     register int16 tot = 0;
  132.     register struct mbuf *bp;
  133.  
  134.     if(bpp == NULLBUFP || *bpp == NULLBUF)
  135.         return; /* Nothing to trim */
  136.  
  137.     if(length == 0){
  138.         /* Toss the whole thing */
  139.         free_p(*bpp);
  140.         *bpp = NULLBUF;
  141.         return;
  142.     }
  143.     /* Find the point at which to trim. If length is greater than
  144.      * the packet, we'll just fall through without doing anything
  145.      */
  146.     for( bp = *bpp; bp != NULLBUF; bp = bp->next){
  147.         if(tot + bp->cnt < length){
  148.             tot += bp->cnt;
  149.         } else {
  150.             /* Cut here */
  151.             bp->cnt = length - tot;
  152.             free_p(bp->next);
  153.             bp->next = NULLBUF;
  154.             break;
  155.         }
  156.     }
  157. }
  158. /* Duplicate/enqueue/dequeue operations based on mbufs */
  159.  
  160. /* Duplicate first 'cnt' bytes of packet starting at 'offset'.
  161.  * This is done without copying data; only the headers are duplicated,
  162.  * but without data segments of their own. The pointers are set up to
  163.  * share the data segments of the original copy. The return pointer is
  164.  * passed back through the first argument, and the return value is the
  165.  * number of bytes actually duplicated.
  166.  */
  167. int16
  168. dup_p(hp,bp,offset,cnt)
  169. struct mbuf **hp;
  170. register struct mbuf *bp;
  171. register int16 offset;
  172. register int16 cnt;
  173. {
  174.     register struct mbuf *cp;
  175.     int16 tot;
  176.  
  177.     if(cnt == 0 || bp == NULLBUF || hp == NULLBUFP){
  178.         if(hp != NULLBUFP)
  179.             *hp = NULLBUF;
  180.         return 0;
  181.     }
  182.     if((*hp = cp = alloc_mbuf(0)) == NULLBUF){
  183.         return 0;
  184.     }
  185.     /* Skip over leading mbufs that are smaller than the offset */
  186.     while(bp != NULLBUF && bp->cnt <= offset){
  187.         offset -= bp->cnt;
  188.         bp = bp->next;
  189.     }
  190.     if(bp == NULLBUF){
  191.         free_mbuf(cp);
  192.         *hp = NULLBUF;
  193.         return 0;       /* Offset was too big */
  194.     }
  195.     tot = 0;
  196.     for(;;){
  197.         /* Make sure we get the original, "real" buffer (i.e. handle the
  198.          * case of duping a dupe)
  199.          */
  200.         if(bp->dup != NULLBUF)
  201.             cp->dup = bp->dup;
  202.         else
  203.             cp->dup = bp;
  204.  
  205.         /* Increment the duplicated buffer's reference count */
  206.         cp->dup->refcnt++;
  207.  
  208.         cp->data = bp->data + offset;
  209.         cp->cnt = min(cnt,bp->cnt - offset);
  210.         offset = 0;
  211.         cnt -= cp->cnt;
  212.         tot += cp->cnt;
  213.         bp = bp->next;
  214.         if(cnt == 0 || bp == NULLBUF || (cp->next = alloc_mbuf(0)) == NULLBUF)
  215.             break;
  216.         cp = cp->next;
  217.     }
  218.     return tot;
  219. }
  220. /* Copy first 'cnt' bytes of packet into a new, single mbuf */
  221. struct mbuf *
  222. copy_p(bp,cnt)
  223. register struct mbuf *bp;
  224. register int16 cnt;
  225. {
  226.     register struct mbuf *cp;
  227.     register char *wp;
  228.     register int16 n;
  229.  
  230.     if(bp == NULLBUF || cnt == 0 || (cp = alloc_mbuf(cnt)) == NULLBUF)
  231.         return NULLBUF;
  232.     wp = cp->data;
  233.     while(cnt != 0 && bp != NULLBUF){
  234.         n = min(cnt,bp->cnt);
  235.         memcpy(wp,bp->data,n);
  236.         wp += n;
  237.         cp->cnt += n;
  238.         cnt -= n;
  239.         bp = bp->next;
  240.     }
  241.     return cp;
  242. }
  243. /* Copy and delete "cnt" bytes from beginning of packet. Return number of
  244.  * bytes actually pulled off
  245.  */
  246. int16
  247. pullup(bph,buf,cnt)
  248. struct mbuf **bph;
  249. char *buf;
  250. int16 cnt;
  251. {
  252.     register struct mbuf *bp;
  253.     int16 n,tot;
  254.  
  255.     tot = 0;
  256.     if(bph == NULLBUFP)
  257.         return 0;
  258.     while(cnt != 0 && (bp = *bph) != NULLBUF){
  259.         n = min(cnt,bp->cnt);
  260.         if(buf != NULLCHAR){
  261.             if(n == 1)      /* Common case optimization */
  262.                 *buf = *bp->data;
  263.             else if(n > 1)
  264.                 memcpy(buf,bp->data,n);
  265.             buf += n;
  266.         }
  267.         tot += n;
  268.         cnt -= n;
  269.         bp->data += n;
  270.         bp->cnt -= n;
  271.         if(bp->cnt == 0){
  272.             /* If this is the last mbuf of a packet but there
  273.              * are others on the queue, return a pointer to
  274.              * the next on the queue. This allows pullups to
  275.              * to work on a packet queue
  276.              */
  277.             if(bp->next == NULLBUF && bp->anext != NULLBUF){
  278.                 *bph = bp->anext;
  279.                 free_mbuf(bp);
  280.             } else
  281.                 *bph = free_mbuf(bp);
  282.         }
  283.     }
  284.     return tot;
  285. }
  286. /* Append mbuf to end of mbuf chain */
  287. void
  288. append(bph,bp)
  289. struct mbuf **bph;
  290. struct mbuf *bp;
  291. {
  292.     register struct mbuf *p;
  293.  
  294.     if(bph == NULLBUFP || bp == NULLBUF)
  295.         return;
  296.     if(*bph == NULLBUF){
  297.         /* First one on chain */
  298.         *bph = bp;
  299.     } else {
  300.         for(p = *bph ; p->next != NULLBUF ; p = p->next)
  301.             ;
  302.         p->next = bp;
  303.     }
  304. }
  305. /* Insert specified amount of contiguous new space at the beginning of an
  306.  * mbuf chain. If enough space is available in the first mbuf, no new space
  307.  * is allocated. Otherwise a mbuf of the appropriate size is allocated and
  308.  * tacked on the front of the chain.
  309.  *
  310.  * This operation is the logical inverse of pullup(), hence the name.
  311.  */
  312. struct mbuf *
  313. pushdown(bp,size)
  314. register struct mbuf *bp;
  315. int16 size;
  316. {
  317.     register struct mbuf *nbp;
  318.  
  319.     /* Check that bp is real, that it hasn't been duplicated, and
  320.      * that it itself isn't a duplicate before checking to see if
  321.      * there's enough space at its front.
  322.      */
  323.     if(bp != NULLBUF && bp->refcnt == 1 && bp->size != 0
  324.      && bp->data - (char *)(bp+1) >= size){
  325.         /* No need to alloc new mbuf, just adjust this one */
  326.         bp->data -= size;
  327.         bp->cnt += size;
  328.     } else {
  329.         nbp = ambufw(size);
  330.         nbp->next = bp;
  331.         nbp->cnt = size;
  332.         bp = nbp;
  333.     }
  334.     return bp;
  335. }
  336. /* Append packet to end of packet queue */
  337. void
  338. enqueue(q,bp)
  339. struct mbuf **q;
  340. struct mbuf *bp;
  341. {
  342.     register struct mbuf *p;
  343.  
  344.     if(q == NULLBUFP || bp == NULLBUF)
  345.         return;
  346.     if(*q == NULLBUF){
  347.         /* List is empty, stick at front */
  348.         *q = bp;
  349.     } else {
  350.         for(p = *q ; p->anext != NULLBUF ; p = p->anext)
  351.             ;
  352.         p->anext = bp;
  353.     }
  354. }
  355. /* Unlink a packet from the head of the queue */
  356. struct mbuf *
  357. dequeue(q)
  358. register struct mbuf **q;
  359. {
  360.     register struct mbuf *bp;
  361.  
  362.     if(q == NULLBUFP)
  363.         return NULLBUF;
  364.     if((bp = *q) != NULLBUF){
  365.         *q = bp->anext;
  366.         bp->anext = NULLBUF;
  367.     }
  368.     return bp;
  369. }
  370.  
  371. /* Copy user data into an mbuf */
  372. struct mbuf *
  373. qdata(data,cnt)
  374. char *data;
  375. int16 cnt;
  376. {
  377.     register struct mbuf *bp;
  378.  
  379.     bp = ambufw(cnt);
  380.     memcpy(bp->data,data,cnt);
  381.     bp->cnt = cnt;
  382.     return bp;
  383. }
  384. /* Copy mbuf data into user buffer */
  385. int16
  386. dqdata(bp,buf,cnt)
  387. struct mbuf *bp;
  388. char *buf;
  389. unsigned cnt;
  390. {
  391.     int16 tot;
  392.     unsigned n;
  393.     struct mbuf *bp1;
  394.  
  395.     if(buf == NULLCHAR)
  396.         return 0;
  397.  
  398.     tot = 0;
  399.     for(bp1 = bp;bp1 != NULLBUF; bp1 = bp1->next){
  400.         n = min(bp1->cnt,cnt);
  401.         memcpy(buf,bp1->data,n);
  402.         cnt -= n;
  403.         buf += n;
  404.         tot += n;
  405.     }
  406.     free_p(bp);
  407.     return tot;
  408. }
  409. /* Pull a 32-bit integer in host order from buffer in network byte order.
  410.  * On error, return 0. Note that this is indistinguishable from a normal
  411.  * return.
  412.  */
  413. int32
  414. pull32(bpp)
  415. struct mbuf **bpp;
  416. {
  417.     char buf[4];
  418.  
  419.     if(pullup(bpp,buf,4) != 4){
  420.         /* Return zero if insufficient buffer */
  421.         return 0;
  422.     }
  423.     return get32(buf);
  424. }
  425. /* Pull a 16-bit integer in host order from buffer in network byte order.
  426.  * Return -1 on error
  427.  */
  428. long
  429. pull16(bpp)
  430. struct mbuf **bpp;
  431. {
  432.     char buf[2];
  433.  
  434.     if(pullup(bpp,buf,2) != 2){
  435.         /* Return -1 if insufficient buffer */
  436.         return -1;
  437.     }
  438.     return get16(buf);
  439. }
  440. /* Pull single character from mbuf */
  441. int
  442. pullchar(bpp)
  443. struct mbuf **bpp;
  444. {
  445.     char c;
  446.  
  447.     if(pullup(bpp,&c,1) != 1)
  448.         return -1;              /* Nothing left */
  449.     return (int)uchar(c);
  450. }
  451. int
  452. write_p(fp,bp)
  453. FILE *fp;
  454. struct mbuf *bp;
  455. {
  456.     while(bp != NULLBUF){
  457.         if(fwrite(bp->data,1,bp->cnt,fp) != bp->cnt)
  458.             return -1;
  459.         bp = bp->next;
  460.     }
  461.     return 0;
  462. }
  463. /* Reclaim unused space in a mbuf chain. If the argument is a chain of mbufs
  464.  * and/or it appears to have wasted space, copy it to a single new mbuf and
  465.  * free the old mbuf(s). But refuse to move mbufs that merely
  466.  * reference other mbufs, or that have other headers referencing them.
  467.  *
  468.  * Be extremely careful that there aren't any other pointers to
  469.  * (or into) this mbuf, since we have no way of detecting them here.
  470.  * This function is meant to be called only when free memory is in
  471.  * short supply.
  472.  */
  473. void
  474. mbuf_crunch(bpp)
  475. struct mbuf **bpp;
  476. {
  477.     register struct mbuf *bp = *bpp;
  478.     struct mbuf *nbp;
  479.  
  480.     if(bp->refcnt > 1 || bp->dup != NULLBUF){
  481.         /* Can't crunch, there are other refs */
  482.         return;
  483.     }
  484.     if(bp->next == NULLBUF && bp->cnt == bp->size){
  485.         /* Nothing to be gained by crunching */
  486.         return;
  487.     }
  488.     if((nbp = copy_p(bp,len_p(bp))) == NULLBUF){
  489.         /* Copy failed due to lack of (contiguous) space */
  490.         return;
  491.     }
  492.     nbp->anext = bp->anext;
  493.     free_p(bp);
  494.     *bpp = nbp;
  495. }
  496.